<?php

/**
 * report actions.
 *
 * @package    student_manager
 * @subpackage report
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class functionActions extends sfActions
{
public function executeConvertDataNumber()  {

    $boms = BOMPeer::doSelect(new Criteria());
    if ($boms && count($boms))  {
        foreach ($boms as $bom) {   
            $number = $bom->getWono();
            $n1 = intval(substr($number, strrpos($number, '-') + 1));
            $number = substr($number, 0, strrpos($number, '-'));
            $year = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            
            $number.= '-'.substr($year, strlen($year) - 2).'-'.sprintf('%03d',$n1);
            $bom->setWono($number);
            $bom->save();            
        }
    }
   echo 'Convert BOM number successfully!<br/>';
   
   
    $boms = MRPeer::doSelect(new Criteria());
    if ($boms && count($boms))  {
        foreach ($boms as $bom) {   
            $number = $bom->getMrno();
            $n1 = intval(substr($number, strrpos($number, '-') + 1));
            $number = substr($number, 0, strrpos($number, '-'));
            $year = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            
            $number.= '-'.substr($year, strlen($year) - 2).'-'.sprintf('%03d',$n1);
            $bom->setMrno($number);
            $bom->save();            
        }
    }
   echo 'Convert MR number successfully!<br/>';
   
   
   
    $boms = MR2Peer::doSelect(new Criteria());
    if ($boms && count($boms))  {
        foreach ($boms as $bom) {   
            $number = $bom->getMrno();
            $n1 = intval(substr($number, strrpos($number, '-') + 1));
            $number = substr($number, 0, strrpos($number, '-'));
            $year = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            
            $number.= '-'.substr($year, strlen($year) - 2).'-'.sprintf('%03d',$n1);
            $bom->setMrno($number);
            $bom->save();            
        }
    }
   echo 'Convert MR sub number successfully!<br/>';
   
   
   
    $boms = MR3Peer::doSelect(new Criteria());
    if ($boms && count($boms))  {
        foreach ($boms as $bom) {   
            $number = $bom->getMrno();
            $n1 = intval(substr($number, strrpos($number, '-') + 1));
            $number = substr($number, 0, strrpos($number, '-'));
            $year = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            
            $number.= '-'.substr($year, strlen($year) - 2).'-'.sprintf('%03d',$n1);
            $bom->setMrno($number);
            $bom->save();            
        }
    }
   echo 'Convert MR Raw number successfully!<br/>';
   
   
   
    $boms = POPeer::doSelect(new Criteria());
    if ($boms && count($boms))  {
        foreach ($boms as $bom) {   
            $number = $bom->getPono();
            $n1 = intval(substr($number, strrpos($number, '-') + 1));
            $number = substr($number, 0, strrpos($number, '-'));
            $year = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            
            $number.= '-'.substr($year, strlen($year) - 2).'-'.sprintf('%03d',$n1);
            $bom->setPono($number);
            $bom->save();            
        }
    }
   echo 'Convert PO number successfully!<br/>';
   
   
   
    $boms = TallyInPeer::doSelect(new Criteria());
    if ($boms && count($boms))  {
        foreach ($boms as $bom) {   
            $number = $bom->getTallyincode();
            $n1 = intval(substr($number, strrpos($number, '-') + 1));
            $number = substr($number, 0, strrpos($number, '-'));
            $month = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            $year = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            
            $number.= '-'.substr($year, strlen($year) - 2).'-'.$month.'-'.sprintf('%03d',$n1);
            $bom->setTallyincode($number);
            $bom->save();            
        }
    }
   echo 'Convert Tallyin number successfully!<br/>';
   $boms = TallyOutPeer::doSelect(new Criteria());
    if ($boms && count($boms))  {
        foreach ($boms as $bom) {   
            $number = $bom->getTallyoutcode();
            $n1 = intval(substr($number, strrpos($number, '-') + 1));
            $number = substr($number, 0, strrpos($number, '-'));
            $month = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            $year = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            
            $number.= '-'.substr($year, strlen($year) - 2).'-'.$month.'-'.sprintf('%03d',$n1);
            $bom->setTallyoutcode($number);
            $bom->save();            
        }
    }
   echo 'Convert Tallyout number successfully!<br/>';
   
   
   $boms =TemporaryOutputPeer::doSelect(new Criteria());
    if ($boms && count($boms))  {
        foreach ($boms as $bom) {   
            $number = $bom->getTempno();
            $n1 = intval(substr($number, strrpos($number, '-') + 1));
            $number = substr($number, 0, strrpos($number, '-'));
            $month = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            $year = substr($number, strrpos($number, '-') + 1);
            $number = substr($number, 0, strrpos($number, '-'));
            
            $number.= '-'.substr($year, strlen($year) - 2).'-'.$month.'-'.sprintf('%03d',$n1);
            $bom->setTempno($number);
            $bom->save();            
        }
    }
   echo 'Convert Temp number successfully!<br/>';
   exit();
}

public function executeExportExcelInventory(){
  	$c = new Criteria();
        $c->addAscendingOrderByColumn(ComponentPeer::CATEGORY);
        $c->addAscendingOrderByColumn(ComponentPeer::ID);
        $compList = ComponentPeer::doSelect($c);
	  	
  	$arrHeader = array("No", "SUNLIGHT Code", "Product Description", "Brand", "Unit", "Opening", "Input", "Output", "Closing", "Min Stock", "Location", "Category");
  	$arrContent = array();
  	
  	$count = count($compList);	
	 
  	$dem =0;
  	for ($i = 0; $i < $count; $i++){
  		$c = new Criteria();
  		$c->add(WarehousePeer::COMPONENT_ID, $compList[$i]->getId());
  		$warehouse = WarehousePeer::doSelectOne($c);
  		if ($warehouse && $warehouse->getClosing() >0){
  			
                    $dem++;
                    $arrContent[$dem][0]= $dem;
                    $arrContent[$dem][1]= $compList[$i]->getProductcode();
                    $arrContent[$dem][2]= $compList[$i]->getDescription();
  			
		    $arrContent[$dem][4]= $compList[$i]->getComponentunit();
		    $arrContent[$dem][3]= $compList[$i]->getBrand();
		    $arrContent[$dem][5]= CommonValues::FormatCurrency($warehouse->getOpening(), FALSE);
		    $arrContent[$dem][6]= CommonValues::FormatCurrency($warehouse->getTotalin(), FALSE);
		    $arrContent[$dem][7]= CommonValues::FormatCurrency($warehouse->getTotalout(), FALSE);
		    $arrContent[$dem][8]= CommonValues::FormatCurrency($warehouse->getClosing(), FALSE);
		    $arrContent[$dem][9]= CommonValues::FormatCurrency($compList[$i]->getMinstockqty(), FALSE);
		    $arrContent[$dem][10]= $compList[$i]->getLocation();
                    $cate = 'MAIN';
                    if( $compList[$i]->getCategory()==1) $cate = 'SUB';
                    elseif ($compList[$i]->getCategory()==2) $cate = 'RAW';
                    $arrContent[$dem][11]= $cate;
  		}
  	}
  	$excel = new ExportExcel("Inventory-".date('y').'-'.date('m').".xls");  	
	$excel->setHeadersAndValues($arrHeader, $arrContent);
	$excel->GenerateExcelFile();
  	echo $dem;
  	exit();
  		
  }

  public function executeExportExcelProvision(){
  	$year = $this->getRequestParameter('year');

        $c = new Criteria();
        $c->addAscendingOrderByColumn(ComponentPeer::ID);
        $complist = ComponentPeer::doSelect($c);
        foreach ($complist as $comp){
            $c = new Criteria();
            $c->add(WarehousePeer::COMPONENT_ID, $comp->getId());
            $wh = WarehousePeer::doSelectOne($c);
            if ($wh && $wh->getClosing() > 0)
                $Sign["".$comp->getId().""]= 1;
        }

        $year_start = date('Y-m-d', strtotime("-$year years"));

        $c = new Criteria();
        $c->add(TallyOutPeer::CREATED_AT, $year_start, Criteria::GREATER_EQUAL);
        $TOList = TallyOutPeer::doSelect($c);
        if ($TOList && count($TOList))
            foreach ($TOList as $TO){
                $c = new Criteria();
                $c->add(ComponentsPeer::COMPONENTGROUP_ID, $TO->getComponentgroupId());
                $compslist = ComponentsPeer::doSelect($c);
                foreach ($compslist as $comps){
                    $Sign["".$comps->getComponentId().""]=0;
                }
            }

        $c = new Criteria();
        $c->add(TallyInPeer::CREATED_AT, $year_start, Criteria::GREATER_EQUAL);
        $TOList = TallyInPeer::doSelect($c);
        if ($TOList && count($TOList))
            foreach ($TOList as $TO){
                $c = new Criteria();
                $c->add(ComponentsPeer::COMPONENTGROUP_ID, $TO->getComponentgroupId());
                $compslist = ComponentsPeer::doSelect($c);
                foreach ($compslist as $comps){
                    $Sign["".$comps->getComponentId().""]=0;
                }
            }
        

  	$arrHeader = array("No", "SUNLIGHT Code", "Product Description", "Brand", "Unit", "Closing");
  	$arrContent = array();  	

  	$dem =0;
  	foreach ($Sign as $key=>$value){
            if ($value==0) continue;
            $comp = ComponentPeer::retrieveByPK($key);
            $c = new Criteria();
            $c->add(WarehousePeer::COMPONENT_ID, $key);
            $warehouse = WarehousePeer::doSelectOne($c);
            if ($warehouse){

                $dem++;
                $arrContent[$dem][0]= $dem;
                $arrContent[$dem][1]= $comp->getProductcode();
                $arrContent[$dem][2]= $comp->getDescription();
                $arrContent[$dem][3]= $comp->getBrand();

                $arrContent[$dem][4]= $comp->getComponentunit();
                $arrContent[$dem][5]= $warehouse->getClosing();
            }
  	}
  	$excel = new ExportExcel("Provision-".date('y').'-'.date('m').".xls");
	$excel->setHeadersAndValues($arrHeader, $arrContent);
	$excel->GenerateExcelFile();
  	echo $dem;
  	exit();

  }
//Export TallyinOr
  public function executeExportExcelTallyIn()
  {
    $this->fromyearexcel = $this->getRequestParameter('fromyearexcel');
    $this->toyearexcel = $this->getRequestParameter('toyearexcel');
    $this->frommonthexcel = $this->getRequestParameter('frommonthexcel');
    $this->tomonthexcel = $this->getRequestParameter('tomonthexcel');
  //  $this->tomonthexcel += ($this->toyearexcel - $this->fromyearexcel)*12; 
    
     $fromDate = "TI-". sprintf("%02d", $this->fromyearexcel % 100)."-". sprintf("%02d", $this->frommonthexcel). "-000";
    $toDate= "TI-". sprintf("%02d",$this->toyearexcel % 100)."-". sprintf("%02d",   $this->tomonthexcel ). "-999";
        
    $this->local = $this->getRequestParameter('local');
    
    //tim kiem
    $c = new Criteria();
    $c->add(TallyInPeer::STATUS, CommonValues::$gTLStatus['key']['Published']);
    $c->add(TallyInPeer::TYPE, CommonValues::$gTallyInType['key']['Supplier']);
    $c->add(TallyInPeer::TALLYINCODE, $fromDate, Criteria::GREATER_EQUAL);
    $c->addAnd(TallyInPeer::TALLYINCODE, $toDate, Criteria::LESS_EQUAL);
    if ($this->local) 
        $c->add(POPeer::TYPE , CommonValues::$gPOType['key']['Local']);
    else
        $c->add(POPeer::TYPE , CommonValues::$gPOType['key']['Overseas']);
    $c->add(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $c->addOr(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['Fully Received']);
    $c->addOr(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['Partially Received']);
    $c->addJoin(TallyInPeer::TALLYNO, POPeer::ID, Criteria::LEFT_JOIN);
    $c->addDescendingOrderByColumn(TallyInPeer::TALLYINCODE);
    
    $ListTI  = TallyInPeer::doSelect($c);
    
    $result = null;
    $this->arrHeader =  array();
    
    //Do du lieu truoc khi tinh toan
    $tomonths =   $this->tomonthexcel  + ($this->toyearexcel - $this->fromyearexcel)*12;
    for ($i = $this->frommonthexcel; $i<= $this->tomonthexcel; $i++){
        $year = $this->fromyearexcel +  intval(($i-1)/12);
        $month = $i % 12;
        $sYear = sprintf("%02d", $year%100);
        $sMonth = sprintf("%02d", $month);
        //$this->arrHeader[$i] = "$sYear-$sMonth";
        $result['Partially']["$sYear-$sMonth"] = 0;
        $result['Fully']["$sYear-$sMonth"] = 0;
        $result['Total']["$sYear-$sMonth"] = 0;
    }
    //thong ke
    foreach ($ListTI as $TI)
    {
        //lay nam va thang
        $TIDate = substr($TI->getTallyincode(), 3, 5);
        $result['Partially']["$TIDate"] += 1;
         $result['Total']["$TIDate"]+=1;
        
        $PO = POPeer::retrieveByPK($TI->getTallyno());
        if ($PO->getReceivedstatus() == CommonValues::$gPOReceivedStatus['key']['Fully Received']
                && !isset($Dau[$PO->getId()]))
        {
            $result['Fully']["$TIDate"] += 1;
            $result['Partially']["$TIDate"] -= 1;
             //$result['Total']["$TIDate"]+=1;
            $Dau[$PO->getId()] = 1;
        }
    }
     //print_r($result);
     $excel = new ExportExcel("TA-IN-".date('y').'-'.date('m').".xls");
     $excel->setResult($result);
     $excel->GenerateExcleFileFromResult();
   
 
  }
//Ham Nay dung de Export Tally Out Meatrial
  public function executeExportTallyoutMat()
  {
    include('filter.php');    
    $c = new Criteria();
    $c->addAscendingOrderByColumn(ProjectPeer::SALENO);
    $ListProject = ProjectPeer::doSelect($c);    
    $resultTemp = null;
    $count = count($ListProject);
    for ($i = 0; $i < $count; $i++)
    {
        for ($j = $this->frommonthexcel; $j<= $tomonths; $j++)
        {
            $year = $this->fromyearexcel +  intval(($j-1)/12);
            $month = $j % 12;
            $sYear = sprintf("%02d", $year%100);
            $sMonth = sprintf("%02d", $month);
           // echo "$sYear-$sMonth";
            
            $resultTemp[$ListProject[$i]->__toString()]["$sYear-$sMonth"] = 0;
        }
    }
        
    //chuan bi de tim kiem
    $fromDate = "TO-". sprintf("%02d",$this->fromyearexcel % 100)."-". sprintf("%02d", $this->frommonthexcel). "-000";
    $toDate= "TO-". sprintf("%02d",$this->toyearexcel % 100)."-". sprintf("%02d", $this->tomonthexcel). "-999";
    
    //tim kiem
    $c = new Criteria();    
    $c->add(TallyOutPeer::STATUS   , CommonValues::$gTLStatus['key']['Published']);
    $c->add(TallyOutPeer::TYPE  , CommonValues::$gTallyOutType['key']['Project']);
    $c->addOr(TallyOutPeer::TYPE  , CommonValues::$gTallyOutType['key']['Production']);
    $c->add(TallyOutPeer::TALLYOUTCODE, $fromDate, Criteria::GREATER_EQUAL);
    $c->addAnd(TallyOutPeer::TALLYOUTCODE, $toDate, Criteria::LESS_EQUAL);
    $TallyoutList  = TallyOutPeer::doSelect($c);   
            
    $count = count($TallyoutList);
    for ($i = 0; $i < $count; $i++)
    {
        //lay nam va thang
        $TODate = substr($TallyoutList[$i]->getTallyoutcode(), 3, 5);
        //echo $TODate."<br>";
        if ($TallyoutList[$i]->getType() == CommonValues::$gTallyOutType['key']['Project'])
        {
            $BOM = BOMPeer::retrieveByPK($TallyoutList[$i]->getTallyno());
            if (!$BOM)                continue;
            
            $resultTemp[$BOM->getProject()->__toString()][$TODate] = 1;
            echo  $resultTemp[$BOM->getProject()->__toString()][$TODate]."<br>";
        }
        else
        {
            $resultTemp[ProjectPeer::retrieveByPK($TallyoutList[$i]->getTallyno())->__toString()][$TODate] = 1;
        }
    }
    echo "TEM ".count($resultTemp);
   for ($i=0;$i<10;$i++)
   {
       for ($j=0;$j<10;$j++)
       {
          echo $resultTemp[$i][$j];
       }
       echo "<br>";
   }
   print_r ($resultTemp);
    $keys = array_keys($resultTemp);    
    $result = null;
    foreach ($keys as $key)
    {        
        for ($j = $this->frommonthexcel; $j<= $tomonths; $j++)
        {
            $year = $this->fromyearexcel +  intval(($j-1)/12);
            $month = $j % 12;
            $sYear = sprintf("%02d", $year%100);
            $sMonth = sprintf("%02d", $month);
            if ($resultTemp[$key]["$sYear-$sMonth"] > 0)
                break;
        }
        if ($j <= $tomonths)
            $result[$key] = $resultTemp[$key];
    }      
    print_r($result);
   // $excel = new ExportExcel("TO-MAT".date('y').'-'.date('m').".xls");
   // $excel->setResult($result);
   // $excel->GenerateExcleFileFromResult();   
    //$this->total = count($keys);
  
}
//Export to Excel MRPO TYPE
public function executeExportMrpoType()
{
   
    $MonthString = array(
    1=>'Jan',
    2=>'Feb',
    3=>'Mar',
    4=>'Apr',
    5=>'May',
    6=>'Jun',
    7=>'Jul',
    8=>'Aug',
    9=>'Sep',
    10=>'Oct',
    11=>'Nov',
    0=>'Dec');
    $this->fromyearexcel = $this->getRequestParameter('fromyearexcel');
    $this->toyearexcel = $this->getRequestParameter('toyearexcel');
    $this->frommonthexcel = $this->getRequestParameter('frommonthexcel');
    $this->tomonthexcel = $this->getRequestParameter('tomonthexcel');
    $this->tomonthexcel += ($this->toyearexcel - $this->fromyearexcel)*12; 
     $c = new Criteria();
    $c->add(MRPeer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);
    $MRList  = MRPeer::doSelect($c);
    
    $c = new Criteria();
    $c->add(MR2Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);
    $MRList = array_merge($MRList, MR2Peer::doSelect($c));
    
    $c = new Criteria();
    $c->add(MR3Peer::APPROVALSTATUS , CommonValues::$gMRApprovalStatus['key']['Approved']);
    $MRList = array_merge($MRList, MR3Peer::doSelect($c));
    
    $c = new Criteria();
    $c->add(POPeer::APPROVALSTATUS  , CommonValues::$gMRApprovalStatus['key']['Approved']);
    $POList = POPeer::doSelect($c);
        
    $count = 0;
    $months = null;
    $result=null;
    $POresult = null;
    $total = null;
    $arrHeader = array();
    $arrHeader1 = array();
    $arrContentMR =  array();
    $arrContentPO  = array();   
    for ($i = $this->frommonthexcel; $i<= $this->tomonthexcel; $i++){
        $year = $this->fromyearexcel +  intval(($i-1)/12);
        $month = $i % 12;
        if ($month > 9)
            $str = "$year-$month-";
        elseif ($month==0)
            $str = "$year-12-";
        else $str="$year-0$month-";
        
        $months[$count] = $i;
        $result[CommonValues::$gMRCategory['key']['Other']][$count] =0;
        $result[CommonValues::$gMRCategory['key']['Stock']][$count] =0;
        $result[CommonValues::$gMRCategory['key']['Factory']][$count] =0;      
        $result[CommonValues::$gMRCategory['key']['Project']][$count] =0;
        
         $POresult[CommonValues::$gMRCategory['key']['Other']][$count] =0; 
        $POresult[CommonValues::$gMRCategory['key']['Stock']][$count] =0;
        $POresult[CommonValues::$gMRCategory['key']['Factory']][$count] =0;       
        $POresult[CommonValues::$gMRCategory['key']['Project']][$count] =0;
       
        foreach ($MRList as $mr){
            $date = $mr->getCreatedAt();
            $date= "date$date";
            $cate = $mr->getCategory();
            if (strpos($date, $str)==4) {
                $result[$cate][$count] += 1;
            }
        }
        
        foreach ($POList as $po){
            $date = $po->getCreatedAt();
            $date= "date$date";
            $cate = $po->getCategory();
            if (strpos($date, $str)==4) {
                $POresult[$cate][$count] += 1;
            }
        }
        
        $count++;
    }
    $row =0;
    $col=1;    
    $arrContentMR[0][0]="ORTHER";
    $arrContentMR[1][0]="STOCK";
    $arrContentMR[2][0]="FACTORY";
    $arrContentMR[3][0]="PROJECT";
    foreach ($result as $k=>$v)
    {
       //$arrContent[$row][$col] = 
      foreach ($v as $l)
      {
           $col++;
         $arrContentMR[$row][$col] = $l."";
        
      }
      $row++;
      $col=1;    
    } 
   
    $col=1;  
    //$row++;
    $arrContentMR[$row][1] = "PO";
    $row++;
   // $arrContentMR[$row][1] = "";
    $dem =0 ;
    foreach ($POresult as $k=>$v)
    {
       switch($dem)
      {
          case 0:  $arrContentMR[$row][0]="ORTHER";
          break;
          case 1:  $arrContentMR[$row][0]="STOCK";
          break;
          case 2:  $arrContentMR[$row][0]="FACTORY";
          break;
          case 3:  $arrContentMR[$row][0]="PROJECT";
          break;
      }
      foreach ($v as $l)
      {
           $col++;
           $arrContentMR[$row][$col] = $l."";          
      }
 
      
      $dem++;
      $row++;   
      $col=1;    
    } 
     $arrHeader[0] = 'MR NAME';    
     $tong = sizeof($months); 
     
    for ($i=0;$i< $tong;$i++)
    {
        $arrHeader[$i+1] = strtoupper($MonthString[$months[$i]]);
        $arrHeader1[$i+1] = strtoupper($MonthString[$months[$i]]);
        
    }
    
    $excel = new ExportExcel("MR-".date('y').'-'.date('m').".xls");
    $excel->setHeadersAndValues($arrHeader, $arrContentMR);
    $excel->GenerateExcelFile();
   //$excel1 = new ExportExcel("PO-".date('y').'-'.date('m').".xls");
   //$excel->setHeadersAndValues($arrHeader1, $arrContentPO);
   //$excel->GenerateExcelFile();
  
  
}
//Ecport excel Local Oversea Qty
public function executeExportValueLO()
{
   $MonthString = array(
    1=>'Jan',
    2=>'Feb',
    3=>'Mar',
    4=>'Apr',
    5=>'May',
    6=>'Jun',
    7=>'Jul',
    8=>'Aug',
    9=>'Sep',
    10=>'Oct',
    11=>'Nov',
    0=>'Dec');
    $this->fromyearexcel = $this->getRequestParameter('fromyearexcel');
    $this->toyearexcel = $this->getRequestParameter('toyearexcel');
    $this->frommonthexcel = $this->getRequestParameter('frommonthexcel');
    $this->tomonthexcel = $this->getRequestParameter('tomonthexcel');
    $this->tomonthexcel += ($this->toyearexcel - $this->fromyearexcel)*12;     
    
    $c = new Criteria();
    $c->add(POPeer::APPROVALSTATUS  , CommonValues::$gMRApprovalStatus['key']['Approved']);
    $POList = POPeer::doSelect($c);
        
    $count = 0;
    $months = null;
    $result=null;
    $Valueresult = null;
    $total = null;
    $arrContent = array();
    $arrHeader = array();
        
       for ($i = $this->frommonthexcel; $i<= $this->tomonthexcel; $i++){
            $year = $this->fromyearexcel+  intval(($i-1)/12);
            $month = $i % 12;
            if ($month > 9)
                $str = "$year-$month-";
            elseif ($month==0)
                $str = "$year-12-";
            else $str="$year-0$month-";
            
            $months[$count] = $i;
            
            $result[CommonValues::$gPOType['key']['Local']][$count] =0;
            $result[CommonValues::$gPOType['key']['Overseas']][$count] =0;
            
            $Valueresult[CommonValues::$gPOType['key']['Local']][$count] =0;
            $Valueresult[CommonValues::$gPOType['key']['Overseas']][$count] =0;
            
            foreach ($POList as $po){
                $date = $po->getCreatedAt();
                $date= "date$date";
                $type = $po->getType();
                $totalprice = $po->getTotalprice();
                if (strpos($date, $str)==4) {
                    $result[$type][$count] += 1;
                    $Valueresult[$type][$count] += $totalprice;
                }
            }
            
            $count++;
        }
    $row =0;
    $col=1;    
    $arrContent[0][0]="SEA";
    $arrContent[1][0]="LOCAL";
    
    foreach ($result as $k=>$v)
    {
       //$arrContent[$row][$col] = 
      foreach ($v as $l)
      {
           $col++;
         $arrContent[$row][$col] = $l."";
        
      }
      $row++;
      $col=1;    
    } 
   
    $col=1;  
    //$row++;
    $arrContent[$row][1] = "";
    $row++;
   // $arrContent[$row][1] = "";
    $dem =0 ;
    foreach ($Valueresult as $k=>$v)
    {
       switch($dem)
      {
          case 0:  $arrContent[$row][0]="OVERSEA";
          break;
          case 1:  $arrContent[$row][0]="LOCAL";
          break;
       
      }
      foreach ($v as $l)
      {
           $col++;
           $arrContent[$row][$col] = $l."";          
      }
 
      
      $dem++;
      $row++;   
      $col=1;    
    } 
     $arrHeader[0] = '';    
     $tong = sizeof($months); 
     
    for ($i=0;$i< $tong;$i++)
    {
        $arrHeader[$i+1] = strtoupper($MonthString[$months[$i]]);
        //$arrHeader1[$i+1] = strtoupper($MonthString[$months[$i]]);
        
    }
    
    $excel = new ExportExcel("LOVALUE-".date('y').'-'.date('m').".xls");
    $excel->setHeadersAndValues($arrHeader, $arrContent);
    $excel->GenerateExcelFile();
}
public function executeExportValuePO()
{
   $MonthString = array(
    1=>'Jan',
    2=>'Feb',
    3=>'Mar',
    4=>'Apr',
    5=>'May',
    6=>'Jun',
    7=>'Jul',
    8=>'Aug',
    9=>'Sep',
    10=>'Oct',
    11=>'Nov',
    0=>'Dec');
    $this->fromyearexcel = $this->getRequestParameter('fromyearexcel');
    $this->toyearexcel = $this->getRequestParameter('toyearexcel');
    $this->frommonthexcel = $this->getRequestParameter('frommonthexcel');
    $this->tomonthexcel = $this->getRequestParameter('tomonthexcel');
    $this->tomonthexcel += ($this->toyearexcel - $this->fromyearexcel)*12;     
    
    $c = new Criteria();
    $c->add(POPeer::APPROVALSTATUS  , CommonValues::$gMRApprovalStatus['key']['Approved']);
    $POList = POPeer::doSelect($c);
        
    $count = 0;
    $months = null;
    $result=null;
    $total = null;
    $arrHeader = array();
    $arrContent = array();    
    for ($i = $this->frommonthexcel; $i<= $this->tomonthexcel; $i++){
        $year = $this->fromyearexcel +  intval(($i-1)/12);
        $month = $i % 12;
        if ($month > 9)
            $str = "$year-$month-";
        elseif ($month==0)
            $str = "$year-12-";
        else $str="$year-0$month-";
        
        $months[$count] = $i;
        
        $result[CommonValues::$gMRCategory['key']['Stock']][$count] =0;
        $result[CommonValues::$gMRCategory['key']['Factory']][$count] =0;
        $result[CommonValues::$gMRCategory['key']['Other']][$count] =0;
        $result[CommonValues::$gMRCategory['key']['Project']][$count] =0;    
        
        foreach ($POList as $po){
            $date = $po->getCreatedAt();
            $date= "date$date";
            $cate = $po->getCategory();
            $totalprice = $po->getTotalprice();
            if (strpos($date, $str)==4) {
                $result[$cate][$count] += $totalprice;
            }
        }
        
        $count++;
    }
    $row =0;
    $col=1;    
    $arrContent[0][0]="STOCK";
    $arrContent[1][0]="FACTORY";
    $arrContent[2][0]="ORTHER";
    $arrContent[3][0]="PROJECT";    
    foreach ($result as $k=>$v)
    {
       //$arrContent[$row][$col] = 
      foreach ($v as $l)
      {
           $col++;
         $arrContent[$row][$col] = $l."";        
      }
      $row++;
      $col=1;    
    }    
     $arrHeader[0] = '';    
     $tong = sizeof($months);
    for ($i=0;$i< $tong;$i++)
    {
        $arrHeader[$i+1] = strtoupper($MonthString[$months[$i]]);  
        
    }
    $excel = new ExportExcel("POVALUE-".date('y').'-'.date('m').".xls");
    $excel->setHeadersAndValues($arrHeader, $arrContent);
    $excel->GenerateExcelFile(); 
}
///
public function executeExportOntimePO()
{
     $MonthString = array(
    1=>'Jan',
    2=>'Feb',
    3=>'Mar',
    4=>'Apr',
    5=>'May',
    6=>'Jun',
    7=>'Jul',
    8=>'Aug',
    9=>'Sep',
    10=>'Oct',
    11=>'Nov',
    0=>'Dec');
    $this->fromyearexcel = $this->getRequestParameter('fromyearexcel');
    $this->toyearexcel = $this->getRequestParameter('toyearexcel');
    $this->frommonthexcel = $this->getRequestParameter('frommonthexcel');
    $this->tomonthexcel = $this->getRequestParameter('tomonthexcel');
    $this->tomonthexcel += ($this->toyearexcel - $this->fromyearexcel)*12;     
    
    
    
    $c = new Criteria();
    $c->add(POPeer::APPROVALSTATUS  , CommonValues::$gMRApprovalStatus['key']['Approved']);
    $POList = POPeer::doSelect($c);
        
    $count = 0;
    $months = null;
    $result=null;
    $total = null;
    $SupplierResult = null;
    $SupplierResult[0]= null;
    $SupplierResult[1]= null;
    $bFirstMonth = true;   
    $arrHeader = array();
    $arrContent  =  array();
       for ($i = $this->frommonthexcel; $i<= $this->tomonthexcel; $i++){
            $year = $this->fromyearexcel +  intval(($i-1)/12);
            $month = $i % 12;
            if ($month > 9)
                $str = "$year-$month-";
            elseif ($month==0)
                $str = "$year-12-";
            else $str="$year-0$month-";
            
            $months[$count] = $i;
            
            $result[0][$count] =0;
            $result[1][$count] =0;           
            
            foreach ($POList as $po){
                $date = $po->getCreatedAt();
                $date= "date$date";
                $expdate = $po->getExparrdate();
                $receiveddate = $po->getReceiveddate();
                $bFullreceived = $po->getReceivedstatus() == CommonValues::$gPOReceivedStatus['key']['Fully Received'];
                $supplierId = $po->getSupplierId();
                if (strpos($date, $str)==4) {
                    if ($receiveddate <= $expdate) {
                        if ($bFullreceived) {
                            $result[1][$count] += 1;     
                            if ($bFirstMonth) {
                                if (!isset($SupplierResult[1]["$supplierId"])) {
                                    $SupplierResult[1]["$supplierId"] =1;
                                } else $SupplierResult[1]["$supplierId"] +=1;
                            }
                        } elseif ($expdate <= date('Y-m-d')) {
                            $result[0][$count] += 1;
                            if ($bFirstMonth) {
                                if (!isset($SupplierResult[0]["$supplierId"])) {
                                    $SupplierResult[0]["$supplierId"] =1;
                                } else $SupplierResult[0]["$supplierId"] +=1;
                            }
                        }
                    } else {
                        $result[0][$count] += 1;     
                        if ($bFirstMonth) {
                            if (!isset($SupplierResult[0]["$supplierId"])) {
                                $SupplierResult[0]["$supplierId"] =1;
                            } else $SupplierResult[0]["$supplierId"] +=1;
                        }
                    }
                }
            }
            
            $count++;
            if ($bFirstMonth) $bFirstMonth = false;
        }
         $row =0;
    $col=1;    
    $arrContent[0][0]="LATE(>1 day)";
    $arrContent[1][0]="OMTIME";   
    foreach ($result as $k=>$v)
    {
       //$arrContent[$row][$col] = 
      foreach ($v as $l)
      {
           $col++;
         $arrContent[$row][$col] = $l."";        
      }
      $row++;
      $col=1;    
    }    
     $arrHeader[0] = '';    
     $tong = sizeof($months);
    for ($i=0;$i< $tong;$i++)
    {
        $arrHeader[$i+1] = strtoupper($MonthString[$months[$i]]);  
        
    }
    $excel = new ExportExcel("PO-ONTIME-".date('y').'-'.date('m').".xls");
    $excel->setHeadersAndValues($arrHeader, $arrContent);
    $excel->GenerateExcelFile(); 
    
    
  
}
 public function executeChangeAllBOMNumber()
{
//    $c = new Criteria();
//    $c->addAscendingOrderByColumn(BOMPeer::ID);
//    $bomlist = BOMPeer::doSelect($c);
//
//    $no = 1;
//    if ($bomlist)
//    {
//        foreach ($bomlist as $bom)
//        {
//            $default = 'BOM-10-';
//            $default.= sprintf('%03d', $no);
//            $bom->setWono($default);
//            $bom->save();
//            $no++;
//        }
//    }

    $c = new Criteria();
    $c->addAscendingOrderByColumn(POPeer::ID);
    $list = POPeer::doSelect($c);

    $no = 1;
    if ($list)
    {
        foreach ($list as $obj)
        {
            $number = $obj->getPono();
            if (!strstr($number, "PO"))
            {
                echo $number;
                echo "<br/>";
            }
        }
    }
    echo "success";
    exit();
}

public function executeListWrongNumber()
{
//    $c = new Criteria();
//    $c->addAscendingOrderByColumn(MRPeer::ID);
//    $list = MRPeer::doSelect($c);
//
//    $no = 1;
//    if ($list)
//    {
//        foreach ($list as $obj)
//        {
//            $number = $obj->getMrno();
//            if (!strstr($number, "MR"))
//            {
//                echo $number;
//                echo "<br/>";
//            }
//        }
//    }
//    exit();


    $c = new Criteria();
    $c->addAscendingOrderByColumn(MR2Peer::ID);
    $list = MR2Peer::doSelect($c);

    $no = 1;
    if ($list)
    {
        foreach ($list as $obj)
        {
            $number = $obj->getMrno();
            if (!strstr($number, "MR"))
            {
                echo $number;
                echo "<br/>";
            }
        }
    }

    $c = new Criteria();
    $c->addAscendingOrderByColumn(MR3Peer::ID);
    $list = MR3Peer::doSelect($c);

    $no = 1;
    if ($list)
    {
        foreach ($list as $obj)
        {
            $number = $obj->getMrno();
            if (!strstr($number, "MR"))
            {
                echo $number;
                echo "<br/>";
            }
        }
    }

    $c = new Criteria();
    $c->addAscendingOrderByColumn(POPeer::ID);
    $list = POPeer::doSelect($c);

    $no = 1;
    if ($list)
    {
        foreach ($list as $obj)
        {
            $number = $obj->getPono();
            if (!strstr($number, "PO"))
            {
                echo $number;
                echo "<br/>";
            }
        }
    }

    $c = new Criteria();
    $c->addAscendingOrderByColumn(TallyInPeer::ID);
    $list = TallyInPeer::doSelect($c);

    $no = 1;
    if ($list)
    {
        foreach ($list as $obj)
        {
            $number = $obj->getTallyincode();
            if (!strstr($number, "TI"))
            {
                echo $number;
                echo "<br/>";
            }
        }
    }
    
    echo "success";
    exit();
}

public function executeChangeTO()
{
    $c = new Criteria();
    $c->addDescendingOrderByColumn(TallyOutPeer::ID);
    $Tolist = TallyOutPeer::doSelect($c);
    foreach ($Tolist as $To){
        $type = $To->getType();
        if ($type == CommonValues::$gTallyOutType['key']['Project'])
        {
            $bom = BOMPeer::retrieveByPK($To->getTallyno());
            if ($bom)
            {
                $To->setProjectId($bom->getProjectId());
            }
        }
        if ($type == CommonValues::$gTallyOutType['key']['Production'])
        {
            $To->setProjectId($To->getTallyno());
        }
        $To->save();
    }
}
public function executeListPO(){

    $c = new Criteria();
    $c->add(TallyOutPeer::TYPE, CommonValues::$gTallyOutType['key']['Project']);

    $TOList = TallyOutPeer::doSelect( $c);
    foreach ($TOList as $to){
        $bom = BOMPeer::retrieveByPK($to->getTallyno());
        if (!$bom){
            echo "TO:".$to->getTallyoutcode()." ID:".$to->getId()." BOMID:".$to->getTallyno()."<br/>";
        }
    }

    echo '=============<br/>';

    
    $c = new Criteria();
    $c->add(BOMPeer::STATUS, CommonValues::$gBomStatus['key']['Revised']);
    $BOMList = BOMPeer::doSelect($c);
    foreach ($BOMList as $Bom)
    {
        echo $Bom->getWono().':'.$Bom->getId().'<br/>';
        $c1 = new Criteria();
        $c1->add(BOMPeer::REVISE_ID, $Bom->getId());
        $bomrvList = BOMPeer::doSelect($c1);
        foreach ($bomrvList as $bomrv){
            echo '----'.$bomrv->getWono().':'.$bomrv->getId().'<br/>';
        }
        $c2 = new Criteria();
        $c2->add(TallyOutPeer::TYPE, CommonValues::$gTallyOutType['key']['Project']);
        $c2->add(TallyOutPeer::TALLYNO, $Bom->getId());
        $ToList = TallyOutPeer::doSelect($c2);
        foreach ($ToList as $to){
            echo '----'.$to->getTallyoutcode().':'.$to->getId().'<br/>';
        }
        $c = new Criteria();
        $c->add(MRPeer::BOM_ID, $Bom->getId());
        $MRList = MRPeer::doSelect($c);
        foreach ($MRList as $MR){
            echo '-------------'.$MR->getMrno().'---'.  CommonValues::$gMRStatus['value'][$MR->getStatus()].'<br/>';
            $c1 = new Criteria();
            $c1->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Main Component']);
            $c1->add(POPeer::MRNO, $MR->getId());
            $POList = POPeer::doSelect($c1);
            foreach ($POList as $PO){
                echo '----------------------------------------'.$PO->getPono().'<br/>';
                $c2 = new Criteria();
                $c2->add(TallyInPeer::TYPE, CommonValues::$gTallyInType['key']['Supplier']);
                $c2->add(TallyInPeer::TALLYNO, $PO->getId());
                $TIList = TallyInPeer::doSelect($c2);
                foreach ($TIList as $TI){
                    echo '----------------------------------------------------------'.$TI->getTallyincode().'<br/>';
                }

            }
        }
    }

        $c = new Criteria();
        $c->add(MR2Peer::STATUS, CommonValues::$gMRStatus['key']['Revised']);
        $MRList = MR2Peer::doSelect($c);
        foreach ($MRList as $MR){
            echo '-------------'.$MR->getMrno().'---'.  $MR->getId().'<br/>';
            $c1 = new Criteria();
            $c1->add(MR2Peer::REVISE_ID, $MR->getId());
            $MRrvList = MR2Peer::doSelect($c1);
            foreach ($MRrvList as $MRrv){
                echo '----'.$MRrv->getMrno().':'.$MRrv->getId().'<br/>';
            }
        
            $c1 = new Criteria();
            $c1->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Sub Component']);
            $c1->add(POPeer::MRNO, $MR->getId());
            $POList = POPeer::doSelect($c1);
            foreach ($POList as $PO){
                echo '----------------------------------------'.$PO->getPono().'<br/>';
                $c2 = new Criteria();
                $c2->add(TallyInPeer::TYPE, CommonValues::$gTallyInType['key']['Supplier']);
                $c2->add(TallyInPeer::TALLYNO, $PO->getId());
                $TIList = TallyInPeer::doSelect($c2);
                foreach ($TIList as $TI){
                    echo '----------------------------------------------------------'.$TI->getTallyincode().'<br/>';
                }

            }
        }
        $c = new Criteria();
        $c->add(MR3Peer::STATUS, CommonValues::$gMRStatus['key']['Revised']);
        $MRList = MR3Peer::doSelect($c);
        foreach ($MRList as $MR){
            echo '-------------'.$MR->getMrno().'---'.  CommonValues::$gMRStatus['value'][$MR->getStatus()].'<br/>';
            $c1 = new Criteria();
            $c1->add(POPeer::COMPCATEGORY, CommonValues::$gCompCategory['key']['Raw Material']);
            $c1->add(POPeer::MRNO, $MR->getId());
            $POList = POPeer::doSelect($c1);
            foreach ($POList as $PO){
                echo '----------------------------------------'.$PO->getPono().'<br/>';
                $c2 = new Criteria();
                $c2->add(TallyInPeer::TYPE, CommonValues::$gTallyInType['key']['Supplier']);
                $c2->add(TallyInPeer::TALLYNO, $PO->getId());
                $TIList = TallyInPeer::doSelect($c2);
                foreach ($TIList as $TI){
                    echo '----------------------------------------------------------'.$TI->getTallyincode().'<br/>';
                }

            }
        }

        echo '-----------------------------------------------<br/>';
        
        $c = new Criteria();
        $c->add(MRPeer::STATUS, CommonValues::$gMRStatus['key']['Revised']);
        $MRList = MRPeer::doSelect($c);
        foreach ($MRList as $MR){
            echo $MR->getMrno().':'.$MR->getId().'<br/>';
            $c1 = new Criteria();
            $c1->add(MRPeer::REVISE_ID, $MR->getId());
            $MR1List = MRPeer::doSelect($c1);
            foreach ($MR1List as $MR1){
                echo '---------'.$MR1->getMrno().':'.$MR1->getId().':'.$MR1->getStatus().'<br/>';
            }
        }

        $c = new Criteria();
        $c->add(MR2Peer::STATUS, CommonValues::$gMRStatus['key']['Revised']);
        $MRList = MR2Peer::doSelect($c);
        foreach ($MRList as $MR){
            echo $MR->getMrno().':'.$MR->getId().'<br/>';
            $c1 = new Criteria();
            $c1->add(MR2Peer::REVISE_ID, $MR->getId());
            $MR1List = MR2Peer::doSelect($c1);
            foreach ($MR1List as $MR1){
                echo '---------'.$MR1->getMrno().':'.$MR1->getId().':'.$MR1->getStatus().'<br/>';
            }
        }

        $c = new Criteria();
        $c->add(MR3Peer::STATUS, CommonValues::$gMRStatus['key']['Revised']);
        $MRList = MR3Peer::doSelect($c);
        foreach ($MRList as $MR){
            echo $MR->getMrno().':'.$MR->getId().'<br/>';
            $c1 = new Criteria();
            $c1->add(MR3Peer::REVISE_ID, $MR->getId());
            $MR1List = MR3Peer::doSelect($c1);
            foreach ($MR1List as $MR1){
                echo '---------'.$MR1->getMrno().':'.$MR1->getId().':'.$MR1->getStatus().'<br/>';
            }
        }

    exit();
}

public function executeCalcWarehouse(){
    $c = new Criteria();
    $c->add(BOMPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $bomList = BOMPeer::doSelect( $c);
    foreach ($bomList as $bom){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(WarehousePeer::COMPONENT_ID, $comps->getComponentId());
                $wh = WarehousePeer::doSelectOne($c1);
                if (!$wh){
                    echo "error ko co component nay:". $comps->getComponentId();
                    exit();
                }
                $wh->setQtyneeded($wh->getQtyneeded() + $comps->getQty1());
                $wh->save();
            }
            
        }
    }

    $c = new Criteria();
    $c->add(MRPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);

    $MRList = MRPeer::doSelect( $c);
    foreach ($MRList as $mr){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(WarehousePeer::COMPONENT_ID, $comps->getComponentId());
                $wh = WarehousePeer::doSelectOne($c1);
                if (!$wh){
                    echo "error ko co component nay:". $comps->getComponentId();
                    exit();
                }
                $wh->setQtyrequested($wh->getQtyrequested() + $comps->getQty1());
                $wh->save();
            }

        }
    }

    $c = new Criteria();
    $c->add(MR2Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);

    $MRList = MR2Peer::doSelect( $c);
    foreach ($MRList as $mr){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(WarehousePeer::COMPONENT_ID, $comps->getComponentId());
                $wh = WarehousePeer::doSelectOne($c1);
                if (!$wh){
                    echo "error ko co component nay:". $comps->getComponentId();
                    exit();
                }
                $wh->setQtyrequested($wh->getQtyrequested() + $comps->getQty1());
                $wh->save();
            }

        }
    }

    $c = new Criteria();
    $c->add(MR3Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);

    $MRList = MR3Peer::doSelect( $c);
    foreach ($MRList as $mr){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(WarehousePeer::COMPONENT_ID, $comps->getComponentId());
                $wh = WarehousePeer::doSelectOne($c1);
                if (!$wh){
                    echo "error ko co component nay:". $comps->getComponentId();
                    exit();
                }
                $wh->setQtyrequested($wh->getQtyrequested() + $comps->getQty1());
                $wh->save();
            }

        }
    }

    $c = new Criteria();
    $c->add(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);

    $POList = POPeer::doSelect($c);

    foreach ($POList as $po){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $po->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(WarehousePeer::COMPONENT_ID, $comps->getComponentId());
                $wh = WarehousePeer::doSelectOne($c1);
                if (!$wh){
                    echo "error ko co component nay:". $comps->getComponentId();
                    exit();
                }
                $wh->setQtyrequested($wh->getQtyrequested() - $comps->getQty1());
                $wh->setQtyordered($wh->getQtyordered() + $comps->getQty1());
                $wh->save();
            }
        }
    }

    echo "success calculated need, request, order";
    exit();
}
public function executeCalcIn(){
    $c = new Criteria();
    $c->add(TallyInPeer::TYPE, CommonValues::$gTallyInType['key']['Supplier']);
    $c->add(TallyInPeer::STATUS, 1);

    $TIList = TallyInPeer::doSelect( $c);
    foreach ($TIList as $ti){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $ti->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(WarehousePeer::COMPONENT_ID, $comps->getComponentId());
                $wh = WarehousePeer::doSelectOne($c1);
                if (!$wh){
                    echo "error ko co component nay:". $comps->getComponentId();
                    exit();
                }

                $wh->setQtyordered($wh->getQtyordered() - $comps->getQty1());
                $wh->save();
            }

        }
    }
    echo "success calculated TI, TO and giam order va need";
	exit();

}
public function executeCalcOut(){
	
    $c = new Criteria();
    $c->add(TallyOutPeer::TYPE, CommonValues::$gTallyOutType['key']['Project']);
    $c->add(TallyOutPeer::STATUS, 1);

    $TOList = TallyOutPeer::doSelect( $c);
    foreach ($TOList as $to){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $to->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(WarehousePeer::COMPONENT_ID, $comps->getComponentId());
                $wh = WarehousePeer::doSelectOne($c1);
                if (!$wh){
                    echo "error ko co component nay:". $comps->getComponentId();
                    exit();
                }

                $wh->setQtyneeded($wh->getQtyneeded() - $comps->getQty1());
                $wh->save();
            }

        }
    }
    echo "success calculated TI, TO and giam order va need";
    exit();
}
public function executeCalcQtyOutstanding(){
    $c = new Criteria();
    $c->add(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $c->add(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['In progress']);
    $c->addOr(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['Fully Received']);
    $c->addOr(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['Partially Received']);

    $POList = POPeer::doSelect($c);
    //echo "stop";
    //exit();
    foreach ($POList as $po){
        if ($po->getCompcategory() ==CommonValues::$gCompCategory['key']['Main Component'])
            $MRobject = MRPeer::retrieveByPK($po->getMrno());
        elseif ($po->getCompcategory() ==CommonValues::$gCompCategory['key']['Sub Component'])
            $MRobject = MR2Peer::retrieveByPK($po->getMrno());
        else
            $MRobject = MR3Peer::retrieveByPK($po->getMrno());

        if (!$MRobject){
            echo "Error khong co MR cho po:".$po->getPono()." id:".$po->getId()."<br/>";
            //exit();
            continue;
        }

        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $po->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
                $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $MRobject->getComponentgroupId());
                $comps1 = ComponentsPeer::doSelectOne($c1);
                if (!$comps1){
                    echo "Error khong co component ".$comps->getComponent(). " id:".$comps->getComponentId()." nay trong mr:".$MRobject->getMrno()." id:".$MRobject->getId()." cua PO:", $po->getPono()." id:".$po->getId()."<br/>";
                    //exit();
                    continue;
                }
                $comps1->setQty4($comps1->getQty4() - $comps->getQty1());
                $comps1->save();
            }

        }
    }

    $c = new Criteria();
    $c->add(TallyInPeer::TYPE, CommonValues::$gTallyInType['key']['Supplier']);
    $c->add(TallyInPeer::STATUS, 1);

    $TIList = TallyInPeer::doSelect( $c);
    foreach ($TIList as $ti){
        $po = POPeer::retrieveByPK($ti->getTallyno());
        if (!$po){
            echo "Error khong co PO cho TI:".$ti->getTallyincode()." id:".$ti->getId()."<br/>";
            continue;
            //exit();
        }
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $ti->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
                $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $po->getComponentgroupId());
                $comps1 = ComponentsPeer::doSelectOne($c1);
                if (!$comps1){
                    echo "Error khong co component".$comps->getComponent(). " id:".$comps->getComponentId()." nay trong po:".$po->getPono(). " id:".$po->getId()." cua TI:".$ti->getTallyincode()." id:".$ti->getId()."<br/>";
                    //exit();
                    continue;
                }
                $comps1->setQty4($comps1->getQty4() - $comps->getQty1());
                $comps1->save();
            }

        }
    }


    $c = new Criteria();
    $c->add(TallyOutPeer::TYPE, CommonValues::$gTallyOutType['key']['Project']);
    $c->add(TallyOutPeer::STATUS, 1);

    $TOList = TallyOutPeer::doSelect( $c);
    foreach ($TOList as $to){
        $bom = BOMPeer::retrieveByPK($to->getTallyno());
        if (!$bom){            
            echo "Error khong co BOM cho TO:".$to->getTallyoutcode()." id:".$to->getId()."<br/>";
            //exit();
            continue;
        }
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $to->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
                $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
                $comps1 = ComponentsPeer::doSelectOne($c1);
                if (!$comps1){
                    echo "Error khong co component".$comps->getComponent()." id:".$comps->getComponentId(). "nay trong BOM:".$bom->getWono()." id:".$bom->getId()." cua TO:". $to->getTallyoutcode()." id:".$to->getId()."<br/>";
                    //exit;
                    continue;
                }
                $comps1->setQty4($comps1->getQty4() - $comps->getQty1());
                $comps1->save();
            }

        }
    }

    echo "success calculated qty outstanding";
    exit();
}
public function executeFindPOWrong(){
    $c = new Criteria();
    $POList = POPeer::doSelect($c);
    foreach ($POList as $po){
        if ($po->getCompcategory() ==CommonValues::$gCompCategory['key']['Main Component'])
                $MRobject = MRPeer::retrieveByPK($po->getMrno());
            elseif ($po->getCompcategory() ==CommonValues::$gCompCategory['key']['Sub Component'])
                $MRobject = MR2Peer::retrieveByPK($po->getMrno());
            else
                $MRobject = MR3Peer::retrieveByPK($po->getMrno());
            if (!$MRobject){
                echo "Error PO khong co MR, ".$po->getPono()." ID:".$po->getId()."<br/>";
            }
            $compgr1 = $po->getComponentgroupId();
            $compgr2 = $MRobject->getComponentgroupId();
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $compgr1);
            $compslist1 = ComponentsPeer::doSelect($c1);
            foreach ($compslist1 as $comps){
                $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
                if (!$comp){
                    echo "Error khong co comp trong he thong, ".$po->getPono()." ID:".$po->getId()."comp:".$comps->getComponentId()."<br/>";
                    continue;
                }
                $c2 = new Criteria();
                $c2->add(ComponentsPeer::COMPONENTGROUP_ID, $compgr2);
                $c2->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
                $exist = ComponentsPeer::doSelectOne($c2);
                if (!$exist){
                    echo "Error comp trong PO khong co trong MR, ".$po->getPono()." ID:".$po->getId()."comp:".$comp->getProductcode().":".$comp->getDescription()."<br/>";
                }
            }
    }
    echo "End";
    exit();
}
public function executeFindTOWrong(){
    $c = new Criteria();
    $c->add(TallyOutPeer::TYPE, CommonValues::$gTallyOutType['key']['Project']);

    $TOList = TallyOutPeer::doSelect( $c);
    foreach ($TOList as $to){
        $bom = BOMPeer::retrieveByPK($to->getTallyno());
        if (!$bom){
            echo "error ko co BOM cho TO:".$to->getTallyoutcode()." ID:".$to->getId()."<br/>";
            continue;
        }
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $to->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "error ko co component".$comps->getComponentId()." nay  ".$to->getComponentgroupId()." trong he thong cua TO:".$to->getTallyoutcode()." ID:".$to->getId()."<br/>";
                continue;
            }
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
                $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
                $comps1 = ComponentsPeer::doSelectOne($c1);
                if (!$comps1){
                    echo "error khong co component".$comps->getComponentId(). " description: ".$comp->getDescription()." nay trong BOM:".$bom->getWono()." ID:".$bom->getId()." cua TO:", $to->getTallyoutcode()." ID:".$to->getId()."<br/>";
                    continue;
                }
            }

        }
    }
    echo "End";
    exit();
}
public function executeFindTIWrong(){
    $c = new Criteria();
    $c->add(TallyInPeer::TYPE, CommonValues::$gTallyInType['key']['Supplier']);

    $TIList = TallyInPeer::doSelect( $c);
    foreach ($TIList as $ti){
        $po = POPeer::retrieveByPK($ti->getTallyno());
        if (!$po){
            echo "error ko co PO cho TI:".$ti->getTallyincode()." ID:".$ti->getId()."<br/>";
            continue;
        }
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $ti->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "error ko co component".$comps->getComponentId()." nay ".$ti->getComponentgroupId()." trong he thong cua TI:".$ti->getTallyincode()." ID:".$ti->getId()."<br/>";
                continue;
            }
            if ($comps->getQty1()>0){
                
                $c1 = new Criteria();
                $c1->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
                $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $po->getComponentgroupId());
                $comps1 = ComponentsPeer::doSelectOne($c1);
                if (!$comps1){
                    echo "error khong co component".$comps->getComponentId(). " description: ".$comp->getDescription()." nay trong PO:".$po->getPono()." ID:".$po->getId()." cua TI:", $ti->getTallyincode()." ID:".$ti->getId()."<br/>";
                    continue;
                }
            }

        }
    }
    echo "end";
    exit();
}
public function executeRemoveRevise(){
    $bomlist = BOMPeer::doSelect(new Criteria());
    foreach ($bomlist as $bom){
        if ($bom->getReviseId()){
            $bom->setReviseId(0);
            $bom->save();
        }
    }
    echo "success";
    exit();
}
public function executeAddBOM(){
    $c = new Criteria();
    $c->add(TallyOutPeer::TYPE, CommonValues::$gTallyOutType['key']['Project']);

    $TOList = TallyOutPeer::doSelect( $c);
    foreach ($TOList as $to){
        $bom = BOMPeer::retrieveByPK($to->getTallyno());
        if (!$bom){
            echo "error ko co BOM cho TO:".$to->getTallyoutcode()." ID:".$to->getId()."<br/>";
            continue;
        }
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $to->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "error ko co component".$comps->getComponentId()." nay  ".$to->getComponentgroupId()." trong he thong cua TO:".$to->getTallyoutcode()." ID:".$to->getId()."<br/>";
                continue;
            }
            if ($comps->getQty1()>0){
                $c1 = new Criteria();
                $c1->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
                $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
                $comps1 = ComponentsPeer::doSelectOne($c1);
                if (!$comps1){
                    echo "add khong co component".$comps->getComponentId(). " description: ".$comp->getDescription()." nay trong BOM:".$bom->getWono()." ID:".$bom->getId()." cua TO:", $to->getTallyoutcode()." ID:".$to->getId()."<br/>";
                    $comps2 = new Components();
                    $comps2->setComponentgroupId($bom->getComponentgroupId());
                    $comps2->setComponentId($comps->getComponentId());
                    $comps2->setQty1($comps->getQty1());
                    $comps2->setQty4($comps->getQty1());
                    $comps2->save();
                }
            }

        }
    }
    echo "Success";
    exit();
}
public function executeCheckMRComplete()
{
    $mrNo = $this->getRequestParameter('mrno');
    $mod = $this->getRequestParameter('mod');
    $id = $this->getRequestParameter('id');
    $list = null;
    $MR = null;
    if ($id != "")
    {
        if ($mod == 'MR2')
            $MR = MR2Peer::retrieveByPK ($id);
        else
            if ($mod == 'MR3')
                $MR = MR3Peer::retrieveByPK ($id);
            else $MR = MRPeer::retrieveByPK ($id);
        if (!$MR)
        {
            echo "Khong co MR nao co id nay";
            exit();
        }
    }
    else
    {
        if ($mod == 'MR2')
        {
            $c = new Criteria();
            $c->add(MR2Peer::MRNO, $mrNo);
            $c->add(MR2Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
            $c->add(MR2Peer::STATUS, CommonValues::$gMRStatus['key']['In progress']);
            $list = MR2Peer::doSelect($c);
        }
        else 
            if ($mod == 'MR3')
            {
                $c = new Criteria();
                $c->add(MR3Peer::MRNO, $mrNo);
                $c->add(MR3Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
                $c->add(MR3Peer::STATUS, CommonValues::$gMRStatus['key']['In progress']);
                $list = MR3Peer::doSelect($c);
            }
            else 
            {
                $c = new Criteria();
                $c->add(MRPeer::MRNO, $mrNo);                
                $c->add(MRPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
                $c->add(MRPeer::STATUS, CommonValues::$gMRStatus['key']['In progress']);
                $list = MRPeer::doSelect($c);
            }
        if (!$list)
        {
            echo "Khong co MR nao co so number nay";
            exit();
        }
        if (count($list) > 1)
        {
            echo "Co nhieu hon 1 MR voi so number nay, xin vui long nhap id cua MR can check";
            exit();
        }
        $MR = $list[0];          
        if (!$MR)
        {
            echo "Khong tim thay MR tuong ung";
            exit();
        }
    }  
    $MRCompgroupID = $MR->getComponentgroupId();
    $c = new Criteria();
    $c->add(ComponentsPeer::COMPONENTGROUP_ID, $MRCompgroupID);
    $MRCompsList = ComponentsPeer::doSelect($c);
    
    foreach ($MRCompsList as $MRComps)
    {
        $c1 = new Criteria();
        $c1->add(POPeer::MRNO, $MR->getId());
        $c1->add(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
        $c1->add(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['In progress']);
        $c1->addOr(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['Fully Received']);
        $c1->addOr(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['Partially Received']);
        if ($mod =='MR2')
            $c1->add(POPeer::COMPCATEGORY, 1);
        else if ($mod =='MR3')
            $c1->add(POPeer::COMPCATEGORY, 2);
        else
            $c1->add(POPeer::COMPCATEGORY, 0);
        $POList = POPeer::doSelect($c1);
        $order = 0;
        foreach ($POList as $PO)
        {
            $c2 = new Criteria();
            $c2->add(ComponentsPeer::COMPONENTGROUP_ID, $PO->getComponentgroupId());
            $c2->add(ComponentsPeer::COMPONENT_ID, $MRComps->getComponentId());
            $POComps = ComponentsPeer::doSelectOne($c2);
            if ($POComps)
                $order += $POComps->getQty1();
        }
        if ($MRComps->getQty1() > $order)
        {
            $outstanding = $MRComps->getQty1() - $order;
            echo "MR nay chua complete <br/>";
            echo "Component ".$MRComps->getComponent()." con ".$outstanding." cai chua order!";
            exit();
        }
    }    
    foreach ($MRCompsList as $MRComps)
    {
        if ($MRComps->getQty4() > 0)
        {
            echo "MR nay da completed nhung he thong tinh sai so luong outstanding, lien he Tien de xem lai!<br/>";
            echo "He thong tinh component ".$MRComps->getComponent()." con ".$MRComps->getQty4()." cai chua order mac du da duoc order roi!";
            exit();
        }
    }
    echo "MR nay da completed!";
    exit();
}
public function executeCompleteObj(){
    //MR
    //PO
    //BOM
    $c = new Criteria();
    $c->add(BOMPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $bomlist = BOMPeer::doSelect($c);
	$dembom =0; $demmr =0; $demmr2 = 0; $demmr3 =0;
    foreach ($bomlist as $bom){

        $c1 = new Criteria();
        $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c1);
        
        $bComplete = true;
        foreach ($compsList as $comps){
            if ($comps->getQty4() > 0){
                $bComplete = false;
            }
        }
        if ($bComplete){
            $bom->setStatus(CommonValues::$gBomStatus['key']['Completed']);
            $bom->save();
            echo $bom->getWono()." complete<br/>";
			$dembom++;
        }
        else if ($bom->getStatus() != 3 && $bom->getStatus() != 4 && $bom->getStatus() != 5){
            $bom->setStatus(CommonValues::$gBomStatus['key']['In progress']);
            $bom->save();
        }
    }

    $c = new Criteria();
    $c->add(MRPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $bomlist = MRPeer::doSelect($c);
    foreach ($bomlist as $bom){

        $c1 = new Criteria();
        $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c1);

        $bComplete = true;
        foreach ($compsList as $comps){
            if ($comps->getQty4() > 0){
                $bComplete = false;
            }
        }
        if ($bComplete){
            $bom->setStatus(CommonValues::$gMRStatus['key']['Completed']);
            $bom->save();
            echo $bom->getMrno()." complete<br/>";
			$demmr++;
        }
        else if ($bom->getStatus() != 3 && $bom->getStatus() != 4 && $bom->getStatus() != 5){
            $bom->setStatus(CommonValues::$gMRStatus['key']['In progress']);
            $bom->save();
        }
    }

    $c = new Criteria();
    $c->add(MR2Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $bomlist = MR2Peer::doSelect($c);
    foreach ($bomlist as $bom){

        $c1 = new Criteria();
        $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c1);

        $bComplete = true;
        foreach ($compsList as $comps){
            if ($comps->getQty4() > 0){
                $bComplete = false;
            }
        }
        if ($bComplete){
            $bom->setStatus(CommonValues::$gMRStatus['key']['Completed']);
            $bom->save();
            echo $bom->getMrno()." complete<br/>";
			$demmr2++;
        }
        else if ($bom->getStatus() != 3 && $bom->getStatus() != 4 && $bom->getStatus() != 5){
            $bom->setStatus(CommonValues::$gMRStatus['key']['In progress']);
            $bom->save();
        }
    }

    $c = new Criteria();
    $c->add(MR3Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $bomlist = MR3Peer::doSelect($c);
    foreach ($bomlist as $bom){

        $c1 = new Criteria();
        $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c1);

        $bComplete = true;
        foreach ($compsList as $comps){
            if ($comps->getQty4() > 0){
                $bComplete = false;
            }
        }
        if ($bComplete){
            $bom->setStatus(CommonValues::$gMRStatus['key']['Completed']);
            $bom->save();
            echo $bom->getMrno()." complete<br/>";
			$demmr3++;
        }
        else if ($bom->getStatus() != 3 && $bom->getStatus() != 4 && $bom->getStatus() != 5){
            $bom->setStatus(CommonValues::$gMRStatus['key']['In progress']);
            $bom->save();
        }
    }

    $c = new Criteria();
    $c->add(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $bomlist = POPeer::doSelect($c);
	$dem1=0; $dem2 = 0; $dem3= 0;
    foreach ($bomlist as $bom){

        $c1 = new Criteria();
        $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c1);

        $bComplete = true;
		$bPartReceived= false;

        foreach ($compsList as $comps){
            if ($comps->getQty4() > 0){
                $bComplete = false;
				if ($comps->getQty4() < $comps->getQty1())
					$bPartReceived=true;
            }
        }
        if ($bComplete){
            $bom->setReceivedstatus(CommonValues::$gPOReceivedStatus['key']['Fully Received']);
            $bom->save();
            echo $bom->getPono()." complete<br/>";
			$dem1++;
        }else if ($bPartReceived){
            $bom->setReceivedstatus(CommonValues::$gPOReceivedStatus['key']['Partially Received']);
            $bom->save();
            echo $bom->getPono()." partireceived<br/>";
			$dem2++;
        }
        else if ($bom->getReceivedstatus() != 4 && $bom->getReceivedstatus() != 5){
            $bom->setReceivedstatus(CommonValues::$gPOReceivedStatus['key']['In progress']);
            $bom->save();
            echo $bom->getPono()." in progress<br/>";
			$dem3++;
        } 
    }
	echo "co $dembom bom complete<br/>";
	echo "co $demmr mr complete<br/>";
	echo "co $demmr2 mr sub complete<br/>";
	echo "co $demmr3 mr raw complete<br/>";
	echo "co $dem1 fully received complete<br/>";
	echo "co $dem2 partreceived<br/>";
	echo "co $dem3 in progress<br/>";
	exit();
}
public function executeFind()
{
    $c = new Criteria();
    $c->add(BOMPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $c->addAnd(BOMPeer::STATUS, CommonValues::$gBomStatus['key']['Completed'], Criteria::NOT_EQUAL);
    $c->addAnd(BOMPeer::STATUS, CommonValues::$gBomStatus['key']['In progress'], Criteria::NOT_EQUAL);
    $bomlist = BOMPeer::doSelect($c);
    foreach ($bomlist as $bom){
        echo $bom->getId().", ";
    }
    echo "<br/>";
    foreach ($bomlist as $bom){
        echo $bom->getStatus().", ";
    }

    echo "<br/>MR<br/>";
    $c = new Criteria();
    $c->add(MRPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $c->addAnd(MRPeer::STATUS, CommonValues::$gMRStatus['key']['Completed'], Criteria::NOT_EQUAL);
    $c->addAnd(MRPeer::STATUS, CommonValues::$gMRStatus['key']['In progress'], Criteria::NOT_EQUAL);
    $bomlist = MRPeer::doSelect($c);
    foreach ($bomlist as $bom){
        echo $bom->getId().", ";
    }
    echo "<br/>";
    foreach ($bomlist as $bom){
        echo $bom->getStatus().", ";
    }

    echo "<br/>MR2<br/>";
    $c = new Criteria();
    $c->add(MR2Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $c->addAnd(MR2Peer::STATUS, CommonValues::$gMRStatus['key']['Completed'], Criteria::NOT_EQUAL);
    $c->addAnd(MR2Peer::STATUS, CommonValues::$gMRStatus['key']['In progress'], Criteria::NOT_EQUAL);
    $bomlist = MR2Peer::doSelect($c);
    foreach ($bomlist as $bom){
        echo $bom->getId().", ";
    }
    echo "<br/>";
    foreach ($bomlist as $bom){
        echo $bom->getStatus().", ";
    }

    echo "<br/>MR3<br/>";
    $c = new Criteria();
    $c->add(MR3Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $c->addAnd(MR3Peer::STATUS, CommonValues::$gMRStatus['key']['Completed'], Criteria::NOT_EQUAL);
    $c->addAnd(MR3Peer::STATUS, CommonValues::$gMRStatus['key']['In progress'], Criteria::NOT_EQUAL);
    $bomlist = MR3Peer::doSelect($c);
    foreach ($bomlist as $bom){
        echo $bom->getId().", ";
    }
    echo "<br/>";
    foreach ($bomlist as $bom){
        echo $bom->getStatus().", ";
    }



    echo "<br/>PO<br/>";
    $c = new Criteria();
    $c->add(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
    $c->addAnd(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['Fully Received'], Criteria::NOT_EQUAL);
    $c->addAnd(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['Partially Received'], Criteria::NOT_EQUAL);
    $c->addAnd(POPeer::RECEIVEDSTATUS, CommonValues::$gPOReceivedStatus['key']['In progress'], Criteria::NOT_EQUAL);
    $bomlist = POPeer::doSelect($c);
    foreach ($bomlist as $bom){
        echo $bom->getId().", ";
    }
    echo "<br/>";
    foreach ($bomlist as $bom){
        echo $bom->getReceivedstatus().", ";
    }
    exit();
}
public function executeFix()
{
    $bomID = array(33, 44, 46, 52, 98, 115, 128, 138, 148, 155, 158, 169, 175, 197, 202, 218, 219, 229, 230, 233, 235, 241, 243, 252, 488, 278, 280, 281, 313, 324, 361, 366, 389, 412, 414, 417, 430, 431, 433, 436, 435, 437, 443, 444, 445, 446, 447, 455, 449, 451, 456, 460, 464, 465, 466, 489, 490, 498);
$bomST= array(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3);
//MR
$mrID= array(201, 26, 199, 30, 36, 196, 195, 82, 92, 105, 108, 113, 131, 186, 145, 157, 159, 162, 206, 208, 217, 245, 419, 282, 246, 247, 293, 331, 335, 349, 364, 365, 369, 377, 379, 380, 384, 385, 386, 388, 389, 390, 394, 397, 399, 403, 404, 405, 420, 421, 433);
$mrST=array(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3);
//MR2
$mr2ID= array(365, 372, 374, 375, 377, 380);
$mr2ST= array(3, 3, 3, 3, 3, 3);
//MR3
$mr3ID= array(67, 80);
$mr3ST= array(3, 3);
//PO
$poID = array(129, 1047, 1424, 1443, 1562);
$poST = array(5, 5, 5, 5, 5);

for ($i = 0; $i < count($bomID); $i++)
{    
    $id = $bomID[$i];
    $bom = BOMPeer::retrieveByPK($id);
    if (!$bom) {
        echo "error khong co bom $id<br/>";
        continue;
    }
    $bom->setStatus($bomST[$i]);
    $bom->save();
}

for ($i = 0; $i < count($mrID); $i++)
{
    $id = $mrID[$i];
    $bom = MRPeer::retrieveByPK($id);
    if (!$bom) {
        echo "error khong co MR $id<br/>";
        continue;
    }
    $bom->setStatus($mrST[$i]);
    $bom->save();
}


for ($i = 0; $i < count($mr2ID); $i++)
{
    $id = $mr2ID[$i];
    $bom = MR2Peer::retrieveByPK($id);
    if (!$bom) {
        echo "error khong co MR2 $id<br/>";
        continue;
    }
    $bom->setStatus($mr2ST[$i]);
    $bom->save();
}

for ($i = 0; $i < count($mr3ID); $i++)
{
    $id = $mr3ID[$i];
    $bom = MR3Peer::retrieveByPK($id);
    if (!$bom) {
        echo "error khong co MR3 $id<br/>";
        continue;
    }
    $bom->setStatus($mr3ST[$i]);
    $bom->save();
}



for ($i = 0; $i < count($poID); $i++)
{
    $id = $poID[$i];
    $bom = POPeer::retrieveByPK($id);
    if (!$bom) {
        echo "error khong co po $id<br/>";
        continue;
    }
    $bom->setReceivedstatus($poST[$i]);
    $bom->save();
}
echo 'success';
            exit();
}
public function executeCheckComp1(){
    $bomList = BOMPeer::doSelect( new Criteria());
    foreach ($bomList as $bom){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "Error khong component".$comps->getComponentId()." trong he thong cua ".$bom->getWono()."<br/>";
            }

        }
    }
    $bomList = MRPeer::doSelect( new Criteria());
    foreach ($bomList as $bom){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "Error khong component".$comps->getComponentId()." trong he thong cua ".$bom->getMrno()."<br/>";
            }

        }
    }
        $bomList = MR2Peer::doSelect( new Criteria());
    foreach ($bomList as $bom){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "Error khong component".$comps->getComponentId()." trong he thong cua ".$bom->getMrno()."<br/>";
            }

        }
    }
	echo "end";
	exit();
}
public function executeCheckComp2(){
		
        $bomList = MR3Peer::doSelect( new Criteria());
    foreach ($bomList as $bom){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "Error khong component".$comps->getComponentId()." trong he thong cua ".$bom->getMrno()."<br/>";
            }

        }
    }
        $bomList = POPeer::doSelect( new Criteria());
    foreach ($bomList as $bom){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "Error khong component".$comps->getComponentId()." trong he thong cua ".$bom->getPono()."<br/>";
            }

        }
    }
    echo "end";
    exit();
}
public function executeCheckComp3(){
		
      $bomList = TallyInPeer::doSelect( new Criteria());
    foreach ($bomList as $bom){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "Error khong component".$comps->getComponentId()." trong he thong cua ".$bom->getTallyincode()."<br/>";
            }

        }
    }
	echo "end";
	exit();
}
public function executeCheckComp4(){

    $bomList = TallyOutPeer::doSelect( new Criteria());
    foreach ($bomList as $bom){
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
        $compsList = ComponentsPeer::doSelect($c);
        foreach ($compsList as $comps){
            $comp = ComponentPeer::retrieveByPK($comps->getComponentId());
            if (!$comp){
                echo "Error khong component".$comps->getComponentId()." trong he thong cua ".$bom->getTallyoutcode()."<br/>";
            }

        }
    }
    echo "end";
    exit();
}
    public function executeCheckCompExist(){
        $id = $this->getRequestParameter('id');
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENT_ID, $id);
        $c->addAscendingOrderByColumn(ComponentsPeer::COMPONENTGROUP_ID);
        $compslist = ComponentsPeer::doSelect($c);
        foreach ($compslist as $comps){
            $c1 = new Criteria();
            $c1->add(MR2Peer::COMPONENTGROUP_ID, $comps->getComponentgroupId());
            $MRList = MR2Peer::doSelect($c1);
            foreach ($MRList as $MR){
                echo $MR->getMrno()."<br/>";
            }

            $c1 = new Criteria();
            $c1->add(POPeer::COMPONENTGROUP_ID, $comps->getComponentgroupId());
            $MRList = POPeer::doSelect($c1);
            foreach ($MRList as $MR){
                echo $MR->getPono()."<br/>";
            }
        }
        echo "end";
        exit();
	}
public function executeCheckMRLink1(){
        $mrlist = MRPeer::doSelect(new Criteria());
        foreach ($mrlist as $mr){
                $bom = BOMPeer::retrieveByPK($mr->getBomId());
                if (!$bom){
                        echo "error".$mr->getMrno();
                }
        }
        $bomlist= BOMPeer::doSelect(new Criteria());
        foreach ($bomlist as $bom){
                $c = new Criteria();
                $c->add(MRPeer::BOM_ID, $bom->getId());
                $mrlist = MRPeer::doSelect($c);
                if (!$mrlist )
                {
                        echo "Warning khong co mr".$bom->getWono()."<br/>";
                }
                else if (count($mrlist) >1)
                {
                        echo "error co nhieu hon 1 mr".$bom->getWono()." ID".$bom->getId()."<br/>";
                        foreach ($mrlist as $mr){
                                echo "----------".$mr->getMrno()." ID".$mr->getId()."<br/>";
                        }
                }
        }
        echo "end";
        exit();
}
public function executeCheckCompInBOM(){
        $id = $this->getRequestParameter('id');
        $comp = ComponentPeer::retrieveByPK($id);
        if (!$comp){
            echo "khong co component nay";
            exit();
        }
        echo "COMPONENT :".$comp->getProductcode(). " DESC: ".$comp->getDescription()."<br/>";
        $c = new Criteria();
        $c->add(BOMPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
        $bomList = BOMPeer::doSelect($c);

        $bomTong =0;
        $mrTong = 0;
        $mr2Tong = 0;
        $mr3Tong = 0;
        $poTong = 0;
        $tiTong =0;
        $toTong = 0;
        foreach ($bomList as $bom){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENT_ID, $id);
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
            $comps = ComponentsPeer::doSelectOne($c1);
            if ($comps){
                $bomTong+= $comps->getQty1();
                echo "SL: ".$comps->getQty1()." ".$bom->getWono()." project".$bom->getProject()."<br/>";
                $c2 = new Criteria();
                $c2->add(MRPeer::BOM_ID, $bom->getId());
                $mr = MRPeer::doSelectOne($c2);
                if ($mr){
                    $c3 = new Criteria();
                    $c3->add(ComponentsPeer::COMPONENT_ID, $id);
                    $c3->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
                    $MRcomps =ComponentsPeer::doSelectOne($c3);
                    if ($MRcomps){
                        $mrTong+= $MRcomps->getQty1();
                        echo "----------------SL: ".$MRcomps->getQty1()." ".$mr->getMrno()."<br/>";
                    } else echo "----co ".$mr->getMrno()." nhung ko tao cho component nay<br/>";
                }else
                    echo "------------KHONG CO MR CHO BOM NAY<br/>";
            }
        }
        $c = new Criteria();
        $c->add(MR2Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
        $mrList = MR2Peer::doSelect($c);

        foreach ($mrList as $mr){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENT_ID, $id);
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
            $comps = ComponentsPeer::doSelectOne($c1);
            if ($comps){
                $mr2Tong+= $comps->getQty1();
                echo "SL: ".$comps->getQty1()." ".$mr->getMrno()."<br/>";
            }
        }

        $c = new Criteria();
        $c->add(MR3Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
        $mrList = MR3Peer::doSelect($c);

        foreach ($mrList as $mr){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENT_ID, $id);
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
            $comps = ComponentsPeer::doSelectOne($c1);
            if ($comps){
                $mr3Tong+= $comps->getQty1();
                echo "SL: ".$comps->getQty1()." ".$mr->getMrno()."<br/>";
            }
        }

        $c = new Criteria();
        $c->add(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
        $mrList = POPeer::doSelect($c);

        foreach ($mrList as $mr){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENT_ID, $id);
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
            $comps = ComponentsPeer::doSelectOne($c1);
            if ($comps){
                $poTong+= $comps->getQty1();
                echo "SL: ".$comps->getQty1()." ".$mr->getPono()."<br/>";
            }
        }

        $c = new Criteria();
        $c->add(TallyInPeer::STATUS, 1);
        $mrList = TallyInPeer::doSelect($c);

        foreach ($mrList as $mr){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENT_ID, $id);
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
            $comps = ComponentsPeer::doSelectOne($c1);
            if ($comps){
                $tiTong+= $comps->getQty1();
                echo "SL: ".$comps->getQty1()." ".$mr->getTallyincode()."<br/>";
            }
        }

        $c = new Criteria();
        $c->add(TallyOutPeer::STATUS, 1);
        $mrList = TallyOutPeer::doSelect($c);

        foreach ($mrList as $mr){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENT_ID, $id);
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
            $comps = ComponentsPeer::doSelectOne($c1);
            if ($comps){
                $toTong+= $comps->getQty1();
                echo "SL: ".$comps->getQty1()." ".$mr->getTallyoutcode()."<br/>";
                if ($mr->getType()==CommonValues::$gTallyOutType['key']['Project']){

                    $bom = BOMPeer::retrieveByPK($mr->getTallyno());
                    if ($bom){
                        echo "-----".$bom->getWono()."<br/>";
                    }
                }
            }
        }
        $c = new Criteria();
        $c->add(WarehousePeer::COMPONENT_ID, $id);
        $Wh = WarehousePeer::doSelectOne($c);
        
        echo "CLOSING ".$Wh->getClosing()." BOM $bomTong MR $mrTong MRsub $mr2Tong MRraw $mr3Tong PO $poTong TI $tiTong TO $toTong end";
        exit();
	}

   public function executeInitQtyOutstanding()
   {
       $c = new Criteria();
       $c->add(BOMPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
       
       $bomlist = BOMPeer::doSelect($c);
       foreach ($bomlist as $bom){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
            $compslist = ComponentsPeer::doSelect($c1);
            foreach ($compslist as $comps){
                $comps->setQty4($comps->getQty1());
                $comps->save();
            }
       }

       $c = new Criteria();
       $c->add(MRPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
       $bomlist = MRPeer::doSelect($c);
       foreach ($bomlist as $bom){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
            $compslist = ComponentsPeer::doSelect($c1);
            foreach ($compslist as $comps){
                $comps->setQty4($comps->getQty1());
                $comps->save();
            }
       }

       $c = new Criteria();
       $c->add(MR2Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
       $bomlist = MR2Peer::doSelect($c);
       foreach ($bomlist as $bom){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
            $compslist = ComponentsPeer::doSelect($c1);
            foreach ($compslist as $comps){
                $comps->setQty4($comps->getQty1());
                $comps->save();
            }
       }
       
       $c = new Criteria();
       $c->add(MR3Peer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
       $bomlist = MR3Peer::doSelect($c);
       foreach ($bomlist as $bom){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
            $compslist = ComponentsPeer::doSelect($c1);
            foreach ($compslist as $comps){
                $comps->setQty4($comps->getQty1());
                $comps->save();
            }
       }

       $c = new Criteria();
       $c->add(POPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);
       $bomlist = POPeer::doSelect($c);
       foreach ($bomlist as $bom){
            $c1 = new Criteria();
            $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
            $compslist = ComponentsPeer::doSelect($c1);
            foreach ($compslist as $comps){
                $comps->setQty4($comps->getQty1());
                $comps->save();
            }
       }
       echo "success";
       exit();
   }
    public function executePrepareWarehouse(){
      $c = new Criteria();
      $whs = WarehousePeer::doSelect($c);
      foreach ($whs as $wh){
          
          $wh->setOpening($wh->getClosing());
          $wh->setTotalin(0);
          $wh->setTotalout(0);
          $wh->setQtyneeded(0);
          $wh->setQtyrequested(0);
          $wh->setQtyordered(0);
          $wh->save();
         
          $comp = ComponentPeer::retrieveByPK($wh->getComponentId());
          if (!$comp)
              $wh->delete();
      }
      echo "success"; exit();
  }

  public function executeDeleteItemFromTO(){
      $toId = $this->getRequestParameter('toId');
      $compId = $this->getRequestParameter('compId');
      $TO = TallyOutPeer::retrieveByPK($toId);
      if (!$TO){
          echo "Tallyout ko co id nay";
          exit();
      }
      if ($TO->getType() != CommonValues::$gTallyOutType['key']['Project']){
          echo "Tallyout nay ko phai cho project";
          exit();
      }

      $bom = BOMPeer::retrieveByPK($TO->getTallyno());
      if (!$bom){
          echo "Khong co BOM cho tallyout nay";
          exit();
      }
      $c = new Criteria();
      $c->add(ComponentsPeer::COMPONENT_ID, $compId);
      $c->add(ComponentsPeer::COMPONENTGROUP_ID, $TO->getComponentgroupId());
      $comps = ComponentsPeer::doSelectOne($c);
      if (!$comps){
          echo "Component id ko co trong tallyout nay";
          exit();
      }

      $c = new Criteria();
      $c->add(ComponentsPeer::COMPONENT_ID, $compId);
      $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
      $comps1 = ComponentsPeer::doSelectOne($c);
      if (!$comps1){
          echo "Component id ko co trong Bom";
          exit();
      }
      $c = new Criteria();
      $c->add(WarehousePeer::COMPONENT_ID, $compId);
      $wh = WarehousePeer::doSelectOne($c);
      if (!$wh){
          echo "Component id ko co trong warehouse";
          exit();
      }
      $wh->setQtyneeded($wh->getQtyneeded() + $comps->getQty1());
      $wh->save();

      $comps1->setQty4($comps1->getQty4() + $comps->getQty1());
      $comps1->save();

      $comps->delete();

      echo "success, da xoa component nay trong tallyout, da tang qty need va tang qty outstanding trong BOM";
      exit();
  }
  public function executeDeleteItemFromTI(){
      $tiId = $this->getRequestParameter('tiId');
      $compId = $this->getRequestParameter('compId');
      $TO = TallyinPeer::retrieveByPK($tiId);
      if (!$TO){
          echo "Tallyin ko co id nay";
          exit();
      }
      if ($TO->getType() != CommonValues::$gTallyInType['key']['Supplier']){
          echo "Tallyin nay ko phai cho Supplier";
          exit();
      }

      $bom = POPeer::retrieveByPK($TO->getTallyno());
      if (!$bom){
          echo "Khong co BOM cho tallyout nay";
          exit();
      }
      $c = new Criteria();
      $c->add(ComponentsPeer::COMPONENT_ID, $compId);
      $c->add(ComponentsPeer::COMPONENTGROUP_ID, $TO->getComponentgroupId());
      $comps = ComponentsPeer::doSelectOne($c);
      if (!$comps){
          echo "Component id ko co trong tallyin nay";
          exit();
      }

      $c = new Criteria();
      $c->add(ComponentsPeer::COMPONENT_ID, $compId);
      $c->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
      $comps1 = ComponentsPeer::doSelectOne($c);
      if (!$comps1){
          echo "Component id ko co trong PO";
          exit();
      }
      $c = new Criteria();
      $c->add(WarehousePeer::COMPONENT_ID, $compId);
      $wh = WarehousePeer::doSelectOne($c);
      if (!$wh){
          echo "Component id ko co trong warehouse";
          exit();
      }
      $wh->setQtyordered($wh->getQtyordered() + $comps->getQty1());
      $wh->save();

      $comps1->setQty4($comps1->getQty4() + $comps->getQty1());
      $comps1->save();

      $comps->delete();

      echo "success, da xoa component nay trong tallyin, da tang qty ordered va tang qty outstanding trong PO";
      exit();
  }
  public function executeListWrongComp(){
      $c = new Criteria();
      $c->addDescendingOrderByColumn(ComponentPeer::ID);
      $listcomp = ComponentPeer::doSelect($c);
      $dem=0;
      echo "COMP co sl > 0 va < 10<br/>";
      foreach ($listcomp as $comp){
          $c = new Criteria();
          $c->add(WarehousePeer::COMPONENT_ID, $comp->getId());
          $wh = WarehousePeer::doSelectOne($c);
          $sl = $wh->getQtyneeded() - $wh->getClosing()-$wh->getQtyrequested() - $wh->getQtyordered() ;
          if ($sl > 0 && $sl < 10){
              $dem++;
              echo "$dem ". $comp->getProductcode()." $sl<br/>";
          }
      }
      echo "----------------------<br/>";
      echo "COMP co sl > 0 va >= 10<br/>";
      foreach ($listcomp as $comp){
          $c = new Criteria();
          $c->add(WarehousePeer::COMPONENT_ID, $comp->getId());
          $wh = WarehousePeer::doSelectOne($c);
          $sl = $wh->getQtyneeded() - $wh->getClosing()-$wh->getQtyrequested() - $wh->getQtyordered() ;
          if ($sl > 0 && $sl >= 10){
              $dem++;
              echo "$dem ". $comp->getProductcode()." $sl<br/>";
          }
      }

      echo "----------------------<br/>";
      echo "COMP co sl < 0 va > -10<br/>";
      foreach ($listcomp as $comp){
          $c = new Criteria();
          $c->add(WarehousePeer::COMPONENT_ID, $comp->getId());
          $wh = WarehousePeer::doSelectOne($c);
          $sl = $wh->getQtyneeded() - $wh->getClosing()-$wh->getQtyrequested() - $wh->getQtyordered() ;
          if ($sl < 0 && $sl > -10){
              $dem++;
              echo "$dem ". $comp->getProductcode()." $sl<br/>";
          }
      }

      echo "----------------------<br/>";
      echo "COMP co sl < 0 va <= -10<br/>";
      foreach ($listcomp as $comp){
          $c = new Criteria();
          $c->add(WarehousePeer::COMPONENT_ID, $comp->getId());
          $wh = WarehousePeer::doSelectOne($c);
          $sl = $wh->getQtyneeded() - $wh->getClosing()-$wh->getQtyrequested() - $wh->getQtyordered() ;
          if ($sl < 0 && $sl <= -10){
              $dem++;
              echo "$dem ". $comp->getProductcode()." $sl<br/>";
          }
      }
      echo "End";
      exit();
  }
  public function executeCalcBeginMonth()
  {
      $this->getResponse()->setTitle('WMS-Month-end-Loop');
      if ($this->getRequest()->isMethod('post')){
          if ($this->getRequestParameter('ok') == 1){
              $WhList = WarehousePeer::doSelect(new Criteria());
              foreach ($WhList as $wh){
                  $wh->setOpening($wh->getClosing());
                  $wh->setTotalin(0);
                  $wh->setTotalout(0);
                  $wh->save();
              }
              $this->finish = 1;
          }
      }
  }
  public function executeTransferPOfromMRrevise(){
      $id = $this->getRequestParameter('id');
      $mod = $this->getRequestParameter('mod');
      
      if ($mod == 'MR'){
          $mr = MRPeer::retrieveByPK($id);
          if (!$mr){
              echo "ERROR: khong co mr nay";
              exit();
          }
          $rvMr = MRPeer::retrieveByPK($mr->getReviseId());
          if (!$rvMr){
              echo "ERROR: khong co revise cho mr nay";
              exit();
          }
      }
      else

      if ($mod == 'MRSUB'){
          $mr = MR2Peer::retrieveByPK($id);
          if (!$mr){
              echo "ERROR: khong co mr nay";
              exit();
          }
          $rvMr = MR2Peer::retrieveByPK($mr->getReviseId());
          if (!$rvMr){
              echo "ERROR: khong co revise cho mr nay";
              exit();
          }
      }
      else


      if ($mod == 'MRRAW'){
          $mr = MR3Peer::retrieveByPK($id);
          if (!$mr){
              echo "ERROR: khong co mr nay";
              exit();
          }
          $rvMr = MR3Peer::retrieveByPK($mr->getReviseId());
          if (!$rvMr){
              echo "ERROR: khong co revise cho mr nay";
              exit();
          }
      }

      $c = new Criteria();
      $c->add(POPeer::MRNO, $rvMr->getId());
      if ($mod == 'MR')
        $c->add(POPeer::COMPCATEGORY, 0);
      else if ($mod =='MRSUB')
          $c->add(POPeer::COMPCATEGORY, 1);
      else $c->add(POPeer::COMPCATEGORY, 2);

      $poList = POPeer::doSelect($c);

      $dem = 0;
      foreach ($poList as $po){
          $po->setMrno($mr->getId());
          $po->save();

          $dem++;

          $c1 = new Criteria();
          $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $po->getComponentgroupId());
          $compsList = ComponentsPeer::doSelect($c1);
          foreach ($compsList as $comps){
              $c2 = new Criteria();
              $c2->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
              $c2->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
              $mrComps = ComponentsPeer::doSelectOne($c2);
              if ($mrComps){
                  $mrComps->setQty4($mrComps->getQty4() - $comps->getQty1());
                  $mrComps->save();
              }

          }
      }
      $c1 = new Criteria();
      $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $mr->getComponentgroupId());
      $compsList = ComponentsPeer::doSelect($c1);

      $bComplete = true;
      foreach ($compsList as $comps){
          if ($comps->getQty4() > 0)
              $bComplete = false;
      }

      if ($bComplete)
      {
        $mr->setStatus(CommonValues::$gBomStatus['key']['Completed']);
        $mr->save();
      }

      echo "Da chuyen $dem POs den MR";
      exit();

  }
  public function executeGetCompsbyclient(){
      $c = new Criteria();
      $c->add(BOMPeer::STATUS, CommonValues::$gBomStatus['key']['In progress']);
      $c->addOr(BOMPeer::STATUS, CommonValues::$gBomStatus['key']['Completed']);
      $c->add(BOMPeer::APPROVALSTATUS, CommonValues::$gMRApprovalStatus['key']['Approved']);

      $bomList = BOMPeer::doSelect($c);
      foreach ($bomList as $bom){
          $c1 = new Criteria();
          $c1->add(ComponentsPeer::COMPONENTGROUP_ID, $bom->getComponentgroupId());
          $c1->add(ComponentsPeer::QTY2, 1);
          $compsList = ComponentsPeer::doSelect($c1);

          if (count($compsList)){
            $componentsByClient = new ComponentsByClient();
            $componentsByClient->setBomId($bom->getId());
            $componentsByClient->setProjectId($bom->getProjectId());
            $componentsByClient->setClientId($bom->getClientId());
            $componentsByClient->setCreatedAt($bom->getCreatedAt());
            //create ComponentGroup
            $componentGroupx = new  ComponentGroup();
            $componentGroupx->setType(1);
            $componentGroupx->save();
            $componentGroupIdx = $componentGroupx->getId();
            $componentsByClient->setComponentgroupId($componentGroupIdx);
            $componentsByClient->setStatus(CommonValues::$gCBCStatus['key']['In progress']);
            $componentsByClient->save();
              foreach ($compsList as $comps){
                  $componentsNew = new Components();
                   $componentsNew->setComponentId($comps->getComponentId());
                   $componentsNew->setComponentgroupId($componentGroupIdx);
                   $componentsNew->setQty1($comps->getQty1());
                   $componentsNew->save();
              }

          }
      }
      echo "Da tach toan bo components by client tu BOM";
      exit();
  }
  public function executeAssignProjectIdforPO()
  {
      $c = new Criteria();
      $c->add(POPeer::COMPCATEGORY,CommonValues::$gCompCategory['key']['Main Component']); 
      //Lay ve cac PO la loai MR Main
      $listPO = POPeer::doSelect($c);
      foreach ($listPO as $po)
      {         
          $mrmain = MRPeer::retrieveByPK($po->getMrno());
          if (!$mrmain)continue;                 
          $bom = BOMPeer::retrieveByPK($mrmain->getBomId());
          if (!$bom) continue;
          $project = $bom->getProject();
          if (!$project) continue;
          $po->setProjectId($project->getId());
          $po->Save();
      }
      echo "Success";
      exit();
  }
   //Gan Id warehouse cho component
  public function executeAssignWarehouseIdforComponent()
  {
      $c = new Criteria();     
      //Lay ve Component
      $listComponent = ComponentPeer::doSelect($c);
      foreach ($listComponent as $comp)
      {         
          $c = new Criteria();
          $c->add(WarehousePeer::COMPONENT_ID,$comp->getId());
          $warehouse = WarehousePeer::doSelectOne($c);
          if (!$warehouse) 
          {
              echo "Khong ton tai warehouse cho component".$comp->getId();
              exit();
          }
          $comp->setWarehouseId($warehouse->getId());        
          $comp->Save();
      }
      echo "Success";
      exit();
  }
  public function executeAssignMrnoforMrnoreal()
  {
      $c = new Criteria();      
      $listPO = POPeer::doSelect($c); 
      foreach ($listPO as $po)
      {
          switch ($po->getCompcategory())
          {
              case CommonValues::$gCompCategory['key']['Main Component']:
                  $mr = MRPeer::retrieveByPK($po->getMrno());
                  if ($mr)
                  {
                      $po->setMrnoreal($mr->getMrno());
                      $po->Save();
                  }
                  break;
              case CommonValues::$gCompCategory['key']['Sub Component']:
                  $mr2 =  MR2Peer::retrieveByPK($po->getMrno());
                  if ($mr2)
                  {
                      $po->setMrnoreal($mr2->getMrno());
                      $po->Save();
                  }
                  break;
              case CommonValues::$gCompCategory['key']['Raw Material']:
                  $mr3 = MR3Peer::retrieveByPK($po->getMrno());
                  if ($mr3)
                  {
                      $po->setMrnoreal($mr3->getMrno());
                      $po->Save();
                  }
                  break;
          }
          
      }
      echo "Success";
      exit();
      
  }
}

